/*
 *
 */

#include <QtGui>

#include "plotarea.h"
#include "mainwindow.h"

MainWindow::MainWindow(char *fileName, vector<float> &data) {
  
  // Resize Window to default values
  this->resize(800,600);
  // Set the size policy for the main window
  QSizePolicy eeSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  eeSizePolicy.setHorizontalStretch(0);
  eeSizePolicy.setVerticalStretch(0);
  eeSizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
  this->setSizePolicy(eeSizePolicy);

  // Create the central widget for the main window
  centralwidget = new QWidget(this);
  eeSizePolicy.setHeightForWidth(centralwidget->sizePolicy().hasHeightForWidth());
  centralwidget->setSizePolicy(eeSizePolicy);

    // Create a layout to place the widgets
  gridLayout = new QGridLayout( centralwidget );
  gridLayout->setSpacing(0);

  // Declare size policies for the widgets
  QSizePolicy ffSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  QSizePolicy feSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
  QSizePolicy efSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  ffSizePolicy.setHorizontalStretch(0);
  ffSizePolicy.setVerticalStretch(0);
  feSizePolicy.setHorizontalStretch(0);
  feSizePolicy.setVerticalStretch(0);
  efSizePolicy.setHorizontalStretch(0);
  efSizePolicy.setVerticalStretch(0);


  // Start creating the widgets to go inside the central widget
  xyDataType = new QLabel( centralwidget );
  // Set the size policy for the widget
  ffSizePolicy.setHeightForWidth(xyDataType->sizePolicy().hasHeightForWidth());
  // Apply the size policy to the widget
  xyDataType->setSizePolicy(ffSizePolicy);
  xyDataType->setMinimumSize(QSize(172,21));
  xyDataType->setMaximumSize(QSize(172,21));
  // Set the text for the label
  xyDataType->setText( "None (U) vs. Time (sec)");
  xyDataType->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
  // Add widget to the layout
  gridLayout->addWidget(xyDataType, 0, 0, 1, 2, Qt::AlignLeft|Qt::AlignVCenter);

  // Names of the files that are being plotted label
  fileNames = new QLabel( centralwidget );

  efSizePolicy.setHeightForWidth(fileNames->sizePolicy().hasHeightForWidth());

  fileNames->setSizePolicy(efSizePolicy);
  fileNames->setMinimumSize(QSize(590,21));
  fileNames->setMaximumSize(QSize(16777215, 21));
  fileNames->setText( QString(fileName) );
  fileNames->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

  gridLayout->addWidget(fileNames, 0, 2, 1, 5, Qt::AlignRight|Qt::AlignVCenter);

  // Legend toggle pushbutton
  legendToggle = new QPushButton( centralwidget );

  ffSizePolicy.setHeightForWidth(legendToggle->sizePolicy().hasHeightForWidth());

  legendToggle->setSizePolicy(ffSizePolicy);
  legendToggle->setMinimumSize(QSize(20,20));
  legendToggle->setMaximumSize(QSize(20,20));
  legendToggle->setText( "L" );

  gridLayout->addWidget(legendToggle, 0, 7, 1, 1, Qt::AlignHCenter|Qt::AlignVCenter);

  // Y Axis Scale Label
  yAxisTick = new QLabel( centralwidget );

  feSizePolicy.setHeightForWidth(yAxisTick->sizePolicy().hasHeightForWidth());

  yAxisTick->setSizePolicy(feSizePolicy);
  yAxisTick->setMinimumSize(QSize(30,0));
  yAxisTick->setMaximumSize(QSize(30,16777215));
  yAxisTick->setText("N/A");
  //yAxisTick->setAlignment(Qt::AlignRight|Qt::AlignTop);

  gridLayout->addWidget(yAxisTick, 1, 0, 1, 1, Qt::AlignRight|Qt::AlignTop);

  // Plot window / frame
  plotFrame = new PlotArea( data, centralwidget );

  eeSizePolicy.setHeightForWidth(plotFrame->sizePolicy().hasHeightForWidth());

  plotFrame->setSizePolicy(eeSizePolicy);
  plotFrame->setFrameShape(QFrame::StyledPanel);
  plotFrame->setFrameShadow(QFrame::Plain);
  plotFrame->setStyleSheet("background-image: url(/home/smithdw/svn/qplot/data/starting_image.png)");
  plotFrame->setLineWidth(2);

  gridLayout->addWidget(plotFrame, 1, 1, 1, 6);

  // Y-Axis Scrollbar for the plotFrame manipulation
  yAxisScroll = new QScrollBar( centralwidget );

  feSizePolicy.setHeightForWidth(yAxisScroll->sizePolicy().hasHeightForWidth());

  yAxisScroll->setSizePolicy(feSizePolicy);
  yAxisScroll->setMinimumSize(QSize(15,0));
  yAxisScroll->setMaximumSize(QSize(15, 16777215));
  yAxisScroll->setOrientation(Qt::Vertical);

  gridLayout->addWidget(yAxisScroll, 1, 7, 1, 1, Qt::AlignRight);

  pHolder3 = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(pHolder3->sizePolicy().hasHeightForWidth());
  pHolder3->setSizePolicy(ffSizePolicy);
  pHolder3->setMinimumSize(QSize(30,25));
  pHolder3->setMaximumSize(QSize(30,25));

  gridLayout->addWidget(pHolder3, 2, 0, 1, 1);

  // X-Axis tick mark labels
  xAxisTick = new QLabel( centralwidget );

  efSizePolicy.setHeightForWidth(xAxisTick->sizePolicy().hasHeightForWidth());

  xAxisTick->setSizePolicy(efSizePolicy);
  xAxisTick->setMinimumSize(QSize(0,25));
  xAxisTick->setMaximumSize(QSize(16777215,25));
  xAxisTick->setText("N/A");
  //xAxisTick->setAlignment(Qt::AlignLeft);

  gridLayout->addWidget(xAxisTick, 2, 1, 1, 6, Qt::AlignLeft|Qt::AlignVCenter);

  // Placeholder to ensure size is correct
  pHolder0 = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(pHolder0->sizePolicy().hasHeightForWidth());
  pHolder0->setSizePolicy(ffSizePolicy);
  pHolder0->setMinimumSize(QSize(20,21));
  pHolder0->setMaximumSize(QSize(20,21));

  gridLayout->addWidget(pHolder0, 2, 7, 1, 1);

  // Add the labels for the mouse position updates
  yMouseLabel = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(yMouseLabel->sizePolicy().hasHeightForWidth());
  yMouseLabel->setSizePolicy(ffSizePolicy);
  yMouseLabel->setMinimumSize(QSize(30,21));
  yMouseLabel->setMaximumSize(QSize(30,21));
  yMouseLabel->setText("y: ");

  yMousePos = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(yMousePos->sizePolicy().hasHeightForWidth());
  yMousePos->setSizePolicy(ffSizePolicy);
  yMousePos->setMinimumSize(QSize(100,21));
  yMousePos->setMaximumSize(QSize(100,21));
  yMousePos->setText("N/A");

  dyMouseLabel = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(dyMouseLabel->sizePolicy().hasHeightForWidth());
  dyMouseLabel->setSizePolicy(ffSizePolicy);
  dyMouseLabel->setMinimumSize(QSize(30,21));
  dyMouseLabel->setMaximumSize(QSize(30,21));
  dyMouseLabel->setText("dy: ");

  dyMousePos = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(dyMousePos->sizePolicy().hasHeightForWidth());
  dyMousePos->setSizePolicy(ffSizePolicy);
  dyMousePos->setMinimumSize(QSize(100,21));
  dyMousePos->setMaximumSize(QSize(100,21));
  dyMousePos->setText("N/A");

  xMouseLabel = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(xMouseLabel->sizePolicy().hasHeightForWidth());
  xMouseLabel->setSizePolicy(ffSizePolicy);
  xMouseLabel->setMinimumSize(QSize(30,21));
  xMouseLabel->setMaximumSize(QSize(30,21));
  xMouseLabel->setText("x: ");

  xMousePos = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(xMousePos->sizePolicy().hasHeightForWidth());
  xMousePos->setSizePolicy(ffSizePolicy);
  xMousePos->setMinimumSize(QSize(100,21));
  xMousePos->setMaximumSize(QSize(100,21));
  xMousePos->setText("N/A");

  dxMouseLabel = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(dxMouseLabel->sizePolicy().hasHeightForWidth());
  dxMouseLabel->setSizePolicy(ffSizePolicy);
  dxMouseLabel->setMinimumSize(QSize(30,21));
  dxMouseLabel->setMaximumSize(QSize(30,21));
  dxMouseLabel->setText("dx: ");

  dxMousePos = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(dxMousePos->sizePolicy().hasHeightForWidth());
  dxMousePos->setSizePolicy(ffSizePolicy);
  dxMousePos->setMinimumSize(QSize(100,21));
  dxMousePos->setMaximumSize(QSize(100,21));
  dxMousePos->setText("N/A");

  gridLayout->addWidget(yMouseLabel,  3, 0, 1, 1, Qt::AlignRight);
  gridLayout->addWidget(yMousePos,    3, 1, 1, 1);
  gridLayout->addWidget(dyMouseLabel, 3, 2, 1, 1);
  gridLayout->addWidget(dyMousePos,   3, 3, 1, 1);
  gridLayout->addWidget(xMouseLabel,  4, 0, 1, 1, Qt::AlignRight);
  gridLayout->addWidget(xMousePos,    4, 1, 1, 1);
  gridLayout->addWidget(dxMouseLabel, 4, 2, 1, 1);
  gridLayout->addWidget(dxMousePos,   4, 3, 1, 1);

  plotInfo = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(plotInfo->sizePolicy().hasHeightForWidth());
  plotInfo->setSizePolicy(ffSizePolicy);
  plotInfo->setMinimumSize(QSize(50,42));
  plotInfo->setMaximumSize(QSize(50,42));
  plotInfo->setText("<html><head><head/><body><p align=\"center\">L=0 Re<br/>(absc)</p></body></html>");

  gridLayout->addWidget(plotInfo, 3, 4, 2, 1);

  // Placeholder to ensure size is correct
  pHolder1 = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(pHolder1->sizePolicy().hasHeightForWidth());
  pHolder1->setSizePolicy(ffSizePolicy);
  pHolder1->setMinimumSize(QSize(20,21));
  pHolder1->setMaximumSize(QSize(20,21));

  gridLayout->addWidget(pHolder1, 3, 7, 1, 1);

  pHolder2 = new QLabel( centralwidget );
  ffSizePolicy.setHeightForWidth(pHolder2->sizePolicy().hasHeightForWidth());
  pHolder2->setSizePolicy(ffSizePolicy);
  pHolder2->setMinimumSize(QSize(20,21));
  pHolder2->setMaximumSize(QSize(20,21));

  gridLayout->addWidget(pHolder2, 4, 7, 1, 1);

  // Add X-Axis scrollbar
  xAxisScroll = new QScrollBar( centralwidget );

  efSizePolicy.setHeightForWidth(xAxisScroll->sizePolicy().hasHeightForWidth());

  xAxisScroll->setSizePolicy(efSizePolicy);
  xAxisScroll->setMinimumSize(QSize(410,15));
  xAxisScroll->setMaximumSize(QSize(16777215,15));
  xAxisScroll->setOrientation(Qt::Horizontal);

  gridLayout->addWidget(xAxisScroll, 3, 5, 2, 2);

  // Set the central widget of the mainwindow
  this->setCentralWidget( centralwidget );

  setWindowTitle(tr("QPlot"));
  centerWindow( this );

  // Connect signals and slots
  connect(plotFrame, SIGNAL(mousePosition(int,int,int,int)),
          this,      SLOT(setMouseLabel(int,int,int,int)));
  //connect(this,      SIGNAL(fileOpen()),
  //        plotFrame, SLOT(placeHolder()));
  connect(plotFrame, SIGNAL(updateLabel(int,int,float*,float*,float*,float*)),
          this,      SLOT(setAxisLabel(int,int,float*,float*,float*,float*)));

  //createActions();
  //createMenus();

}

void MainWindow::createActions() {

  newAction    = new QAction(tr("&New"), this);
  openAction   = new QAction(tr("&Open..."), this);
  saveAction   = new QAction(tr("&Save"), this);
  exitAction   = new QAction(tr("E&xit"), this);
  saveAsAction = new QAction(tr("S&aveas..."), this);

  connect(newAction,    SIGNAL(triggered()), this, SLOT(newFile()));
  connect(openAction,   SIGNAL(triggered()), this, SLOT(open()));
  connect(exitAction,   SIGNAL(triggered()), qApp, SLOT(quit()));
  connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

}

void MainWindow::createMenus() {

	fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(newAction);
	fileMenu->addAction(openAction);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAction);

}

void MainWindow::centerWindow( QMainWindow *window) {
  
  int x,y;
  int screenWidth, screenHeight;

  // Default values
  int WIDTH  = 800;
  int HEIGHT = 600;

  QDesktopWidget *desktop = QApplication::desktop();
  QRect screen            = desktop->availableGeometry();
  // Get the position, width, and height of the window
  screen.getRect( &x, &y, &screenWidth, &screenHeight );

  x = (screenWidth - WIDTH) / 2;
  y = (screenHeight - HEIGHT) / 2;

  window->setGeometry( x, y, WIDTH, HEIGHT );
}

void MainWindow::setMouseLabel( int xPos, int yPos, int dxPos, int dyPos) {

    // Update the labels text with current mouse position
    yMousePos->setText(QString::number(yPos));
    xMousePos->setText(QString::number(xPos));
    // Update the differential labels text with current - stored position
    dyMousePos->setText(QString::number(dyPos));
    dxMousePos->setText(QString::number(dxPos));
}

void MainWindow::setAxisLabel( int numXTicks, int numYTicks, float *xLoc, float *yLoc, float *xVals, float *yVals) {

    stringstream xHtml;
    stringstream yHtml;

    // Build the X-Axis Label
    xHtml << "<html><head></head><body>"
          << "<table width=\"" << xLoc[numXTicks-1] - xLoc[0] << "\" height=\"" << xAxisTick->height()
          << "\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
          << "<tr><td align=\"left\" width=\"" << (xLoc[1]-xLoc[0])/2  << "\">"
          << xVals[0] << "</td>" << "<td align=\"right\" width=\"" << (xLoc[1] - xLoc[0])/2
          << "\">" << xVals[1] << "</td>";

    for(int dx = 2; dx < numXTicks; dx++) {
        xHtml << "<td align=\"right\" width=\"" << xLoc[dx] - xLoc[dx-1] << "\">"
              << xVals[dx] << "</td>";
    }
    xHtml << "</tr></table></body></html>";
    // Build the Y-Axis Label
    yHtml << "<html><body>"
          << "<table width=\"" << yAxisTick->width() << "\" height=\"" << yLoc[numYTicks-1] - yLoc[0]
          << "\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">"
          << "<tr><td align=\"center\" valign=\"top\" height=\"" << floor((yLoc[1] - yLoc[0])/2) << "\">"
          << yVals[0] << "</td></tr>" << "<tr><td align=\"center\" valign=\"bottom\" height=\""
          << ceil((yLoc[2] - yLoc[1])/2)  << "\">"
          << yVals[1] << "</td></tr>";
    for(int dx = 2; dx < numYTicks; dx++) {
        yHtml << "<tr><td align=\"center\" valign=\"bottom\" height=\""  << yLoc[dx] - yLoc[dx-1] << "\">"
              << yVals[dx] << "</td></tr>";
    }
    yHtml << "</table></body></html>";

    //xAxisTick->setText(QString::fromStdString(xHtml.str()));
    //yAxisTick->setText(QString::fromStdString(yHtml.str()));

}

void MainWindow::newFile() {
}
void MainWindow::help() {
}
void MainWindow::about() {
}
bool MainWindow::saveAs() {
  return true;
}
bool MainWindow::save() {
  return true;
}
void MainWindow::open() {

    /*cout << "Width:  " << centralWidget()->width() << endl;
    cout << "Height: " << centralWidget()->height() << endl;
    emit fileOpen();*/
}
void MainWindow::closeEvent( QCloseEvent *event ) {
}
